package pub.gll.onepeas.libbase.track.model;


import android.view.View;

import pub.gll.onepeas.libbase.track.api.IViewExposure;

import java.util.HashMap;

/**
 * <pre>
 *     @author : whg
 *     @e-mail : guanhuawei@kkworld.com
 *     @time   : 2021/7/12 16:27
 *     @desc   : 曝光数据model
 *     @version: 1.0
 * </pre>
 */
public class ExposureModel implements Cloneable {
    public View view;
    public Object tag;
    public float dimThreshold;
    public long beginTime = 0;
    public long endTime = 0;

    public HashMap<String, Object> params;

    public IViewExposure listener;

    ExposureModel next;
    public static final Object sPoolSync = new Object();
    private static ExposureModel sPool;
    private static int sPoolSize = 0;
    private static final int MAX_POOL_SIZE = 50;

    public static ExposureModel obtain() {
        synchronized (sPoolSync) {
            if (sPool != null) {
                ExposureModel m = sPool;
                sPool = m.next;
                m.next = null;
                sPoolSize--;
                return m;
            }
        }
        return new ExposureModel();
    }


    public static ExposureModel obtain(ExposureModel orig) {
        ExposureModel m = obtain();
        m.view = orig.view;
        m.tag = orig.tag;
        m.beginTime = orig.beginTime;
        m.endTime = orig.endTime;
        m.params = orig.params;
        m.listener = orig.listener;
        m.dimThreshold = orig.dimThreshold;
        return m;
    }


    public void recycleUnchecked() {
        view = null;
        tag = null;
        params = null;
        listener = null;
        beginTime = 0;
        endTime = 0;
        dimThreshold = 0f;
        synchronized (sPoolSync) {
            if (sPoolSize < MAX_POOL_SIZE) {
                next = sPool;
                sPool = this;
                sPoolSize++;
            }
        }
    }

    /**
     * 深拷贝
     *
     * @return
     */
    @Override
    public Object clone() {
        ExposureModel exposureModel = null;

        try {
            exposureModel = (ExposureModel) super.clone();
        } catch (CloneNotSupportedException e) {

        }

        if (exposureModel != null && this.params != null) {
            exposureModel.params = (HashMap<String, Object>) this.params.clone();
        }

        return exposureModel;
    }
}
